import validHtml from 'ngtemplate!html!../../../tpls/dialog/valid.dialog.html';

class TeamEditController {
    constructor($rootScope, $scope, $cookieStore, $timeout, $state, ngDialog, dialogService, TeamService, getCourseList, getTeamDetail, getTeamRunner, iRunner){

        var vm = this;

        vm.$rootScope    = $rootScope;
        vm.$cookieStore  = $cookieStore;
        vm.$timeout      = $timeout;
        vm.$state        = $state;
        vm.ngDialog      = ngDialog;
        vm.dialogService = dialogService;
        vm.TeamService   = TeamService;
        vm.getCourseList = getCourseList;
        vm.getTeamDetail = getTeamDetail;
        vm.getTeamRunner = getTeamRunner;
        vm.iRunner       = iRunner;

        // 团队性别
        vm.team_gender = iRunner.teamGender;

        // 科目列表
        vm.getCourseList.code==0&&(vm.courselist = vm.getCourseList.data);

        // 团队成员列表
        vm.getTeamRunner.code==0&&(vm.runnerlist = vm.getTeamRunner.data);
        
        // 表单数据初始化
        vm.team = {
            team_name: '',
            team_gender: '',
            course: ''
        }

        console.log(vm.getTeamDetail)

        // 获取分组详情
        if (vm.getTeamDetail.code==0) {
            var Detail = vm.getTeamDetail.data;
            vm.team.team_name   = Detail.team_name;

            angular.forEach(vm.courselist, function(value, index){
                if (Detail.course_id == value.course_id) {
                    vm.team.course = value;
                }
            });

            angular.forEach(vm.team_gender, function(value, index){
                if (Detail.team_gender == value.id) {
                    vm.team.team_gender = value;
                }
            });
        }

    }


    // 表单提交
    submitForm(isValid) {

        var vm = this;

        // 验证失败, 提示用户
        if (!isValid) {
            if (!vm.team.course) {
                $.blink(angular.element(document.getElementsByName('course')), 5, 100)
            } else if (!vm.team.team_name) {
                $.blink(angular.element(document.getElementsByName('teamName')), 5, 100)
            } else if (!vm.team.team_gender) {
                $.blink(angular.element(document.getElementsByName('teamGender')), 5, 100)
            }
            return false;
        }

        // 验证通过
        if (isValid) {

            // 拼接POST数据格式
            var params = {
                team_name: vm.team.team_name,
                team_gender: vm.team.team_gender.id,
                course_id: vm.team.course.course_id
            }

            console.log(params)

            // 更新分组
            vm.TeamService.postTeamUpdate(params, vm.$cookieStore.get('access_token'), vm.$state.params.team_id)
            .then( data => {

                // 成功调用
                data.code==0&&vm.dialogService.success('更新成功', 'dashboard.race.teamlist');

                // 调用失败, 提示用户
                (data.code!=0&&data.code!=-1001)&&vm.dialogService.error(data.message);

                // 登录信息已过期, 请重新登录
                data.code==-1001&&vm.dialogService.error(data.message, true);
                
                console.log(data)
            }, error => console.log('error'));
        }

        return false;
    }

    // 删除团队
    deleteItems(team_id) {
        
        var vm = this;

        // 模态框显示文本
        vm.$rootScope.common = {
            text : '是否删除团队信息'
        }

        vm.ngDialog.open({
            template: validHtml,
            className: 'ngdialog-theme-default ngdialog-alert',
            scope: vm.$rootScope
        });

        // 提交表单
        vm.$rootScope.submitDelForm = (isValid) => {

            // 验证失败, 提示用户
            if (!isValid) {
                vm.dialogService.error();
            }

            // 验证通过
            if (isValid) {

                // 关闭模态框
                vm.ngDialog.close();

                vm.TeamService.postTeamDel({team_id: team_id}, vm.$cookieStore.get('access_token'), vm.$state.params.race_id)
                .then( data => {
                    
                    // 成功调用
                    data.code==0&&vm.dialogService.success('团队删除成功', 'dashboard.race.teamlist');
                    
                    // 调用失败, 提示用户
                    (data.code!=0&&data.code!=-1001)&&vm.dialogService.error(data.message);

                    // 登录信息已过期, 请重新登录
                    data.code==-1001&&vm.dialogService.error(data.message, true);

                    console.log(data)
                }, error => console.log('error'));
            }

            return false;
        }  
    }
}

TeamEditController.$inject = ['$rootScope', '$scope', '$cookieStore', '$timeout', '$state', 'ngDialog', 'dialogService', 'TeamService', 'getCourseList', 'getTeamDetail', 'getTeamRunner', 'iRunner']; 

export default TeamEditController;